// pages/zdPark/zdPark.js
var util = require('../../utils/request.js');
var globalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //订单信息
    orderInfo: {},
    noOrder: true,
    cardId: '',
    card: [],
    current: '0',
    animation: ''
  },
  selectCoupon: function () {
    wx.navigateTo({
      url: '../coupon/coupon',
    })
  },
  changeSelect: function (e) {
    var that = this
    this.setData({
      current: e.detail.current
    })
    console.log(this.data.card.length)
    if (that.data.card[e.detail.current].ID != null) {
      this.setData({
        cardId: that.data.card[e.detail.current].licensePlateId
      })
      this.getOrder();
    } else {
      this.setData({
        noOrder: true
      })
    }
    console.log(this.data.current);
  },
  //去支付
  goPay: function () {
    var that = this
    util.req('/order/finish', { sn: this.data.orderInfo.sn, memberId: globalData.memberId, parkType: this.data.orderInfo.parkType }, function (success) {
      if (success.resultCode == 200) {
        wx.redirectTo({
          url: '../payOrder/payOrder?sn=' + that.data.orderInfo.sn,
        })
      } else if(res.resultCode == 400) {
        wx.showToast({
          title: res.resultMsg,
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '结束订单错误！',
          icon: 'none'
        })
      }
    })

  },
  // 点击车牌
  addCard: function (e) {
    var that = this;
    if (e.currentTarget.id == '') {
      // 添加
      wx.navigateTo({
        url: '../addCard/addCard',
      })
    } else {
      // 修改、删除
      wx.showActionSheet({
        itemList: ['修改', '删除'],
        success: function (res) {
          console.log(res)
          var cardNumber = that.data.card[that.data.current];
          console.log(cardNumber);
          if (res.tapIndex == 0) {
            // 修改
            wx.navigateTo({
              url: '../addCard/addCard?number=' + cardNumber.number + '&licensePlateId=' + cardNumber.licensePlateId + '&type=' + cardNumber.type,
            })
          } else if (res.tapIndex == 1) {
            // 删除
            console.log(cardNumber);
            util.req('/licensePlate/delLicensePlate', { 'licensePlateId': cardNumber.licensePlateId, 'memberId': globalData.memberId }, function (data) {
              console.log(data);
              if (data.resultCode == 200) {
                wx.showToast({
                  title: data.resultMsg,
                })
                that.getCard();
              } else {
                wx.showToast({
                  title: data.resultMsg,
                  icon: 'none'
                })
              }
            })
          }
        },
        fail: function (res) {
          console.log(res.errMsg)
        }
      })
    }
  },
  // 获取车牌列表
  getCard() {
    var that = this;
    util.req('/licensePlate/myLicensePlate', { 'memberId': globalData.memberId }, function (res) {
      console.log(res);
      if (res.resultContent.length <= 0) {
        wx.redirectTo({
          url: '../addCard/addCard',
        })
      } else {
        var card = [];
        res.resultContent.forEach(function (item) {
          item.name = item.number.slice(0, 2);
          item.ID = item.number.slice(2);
          card.push(item);
        })
        if (card.length <= 9) {
          card.push({ type: 'ordinary', hasCard: false });
        }
        console.log(card)
        that.setData({
          card: card
        })
        if (card.length != 0) {
          that.setData({
            cardId: card[0].licensePlateId
          })
          that.getOrder();
        }
      }
    })
  },
  //刷新缴费信息
  refreshOrder: function () {
    var that = this;
    this.setData({
      animation: true,
    })
    setTimeout(function() {
      that.setData({
        animation: '',
      })
    }, 1000)
    this.getOrder();
  },
  //获取实时缴费信息
  getOrder: function () {
    var that = this
    var name = that.data.card[that.data.current].number;
    // console.log(name);
    util.req('/order/refreshParkingOrder', { number: name }, function (success) {
      console.log(success)
      if (success.resultCode == 200) {
        var orderInfo = {
          finishDate: util.formatTime(success.resultContent.finishDate),
          orderDuration: util.formatSeconds(success.resultContent.orderDuration),
          business: success.resultContent.business,
          useDate: util.formatTime(success.resultContent.useDate),
          card: success.resultContent.number,
          amount: Math.floor(success.resultContent.amount * 100) / 100,
          parkType: success.resultContent.parkType,
          sn: success.resultContent.sn
        }
        that.setData({
          orderInfo: orderInfo,
          noOrder: false
        })
      } else {
        that.setData({
          noOrder: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    this.getCard();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})